package com.hexacta.hrs.sandbox.tasks2.service;

import com.hexacta.hrs.sandbox.tasks2.data.ActivityDao;
import com.hexacta.hrs.sandbox.tasks2.data.TaskDao;
import com.hexacta.hrs.sandbox.tasks2.data.UserDao;
import com.hexacta.hrs.sandbox.tasks2.model.Activity;
import com.hexacta.hrs.sandbox.tasks2.model.Task;
import com.hexacta.hrs.sandbox.tasks2.model.User;

public class UserServiceImpl implements UserService {

	private UserDao userDao;

	private ActivityDao activityDao;
	
	private TaskDao taskDao;

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public ActivityDao getActivityDao() {
		return activityDao;
	}

	public void setActivityDao(ActivityDao activityDao) {
		this.activityDao = activityDao;
	}

	public User createUser(String name) {
		return new User(name);
	}

	public User findByName(String name) {
		return this.getUserDao().findByName(name);
	}

	public void addUser(User user) {
		this.getUserDao().addUser(user);
	}

	public void addActivity(User user, Activity activity) {
		user.addActivity(activity);
		this.getUserDao().persistUser(user);
	}

	/**
	 * busca usuario segun id
	 */
	public User findById(Long id) {
		return this.getUserDao().findById(id);

	}

	/**
	 * busca una actividad del usuario, segun el nombre de la actividad
	 */
	public Activity findActivityByName(User user, String selectedActivity) {

		for (Activity activity : user.getActivities()) {
			if (activity.getName().equals(selectedActivity)) {
				this.getActivityDao().initialize(activity);
				return activity;
			}
		}
		return null;
	}

	public void addTask(Activity activity, Task task) {

		activity.addTask(task);
		this.getActivityDao().persistActivity(activity);

	}

	public Activity findActivityById(Long long1) {
		Activity activity = this.getActivityDao().findById(long1);
		this.getActivityDao().initialize(activity);
		return activity;
	}

	public Task findTaskById(Long long1) {
		
		return this.getTaskDao().findById(long1);
	}

	public void setTaskDao(TaskDao taskDao) {
		this.taskDao = taskDao;
	}

	public TaskDao getTaskDao() {
		return taskDao;
	}

	public void modifyTaskDuration(Task task, int parseInt) {
		task.setTimeLength(parseInt);
		
		this.getTaskDao().persistTask(task);
		
	}

}
